/*===========================================================
 
 .---=||||[ B L O O D Y ]|||||=====================----------....
 |                                                              |
 |    THE  M O S T   B L O O D Y  GAME EVER!                    |
 |                                                              |
 |    bloody harvest is coming... soon,                         |
 |                                              heil Satan!     |
 !                                                              !
 .                                                              ,
  ~~~~-------------================================|||||||||==-~




 Name-Space: {core, logic, editor}
 Module: {draw, coll, scgr, ...}
 Class: <name of class>
 Last modification:  <author>, <time>

 TODO:
 <-------- todo #1 ---------->
 <-------- todo #2 ---------->
 <-------- todo #3 ---------->
        ............
 <-------- todo #n ---------->

 FIX:
 <-------- fix #1 ----------->
 <-------- fix #1 ----------->
         ..........
 <-------- fix #n ----------->




 
 
 ============================================================*/
#include "core/draw/TextureManager.h"
#include "core/draw/Image.h"
using namespace std;

namespace core
{
   namespace draw
   {
     
     
     
      TextureManager* TextureManager::inst = 0;
     
      TextureManager::TextureManager()
      {
      }
     
      TextureManager::~TextureManager()
      {
	 for (IdList::iterator it = idList.begin(); it != idList.end(); it++)
	 {
	    delete (*it).second;
	 }
      }	
	
      unsigned int TextureManager::loadTexture(string fileName)
      {
	 Image img;
	 img.load(fileName.c_str());
	 unsigned int ret;
	 glGenTextures(1, &ret);
	 glBindTexture(GL_TEXTURE_2D, ret);
	 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	 glTexImage2D(GL_TEXTURE_2D, 0, 4, img.getWidth(), img.getHeight(), 0, 
		      img.getGLType(), GL_UNSIGNED_BYTE, img.getBitmap());
			
	 return ret;
      }
		
      Texture* TextureManager::add(string fileName)
      {
	 if (textureList.find(fileName) != textureList.end())
	 {
	    Texture* tex = textureList[fileName];
	    cerr <<"reused texture id: "<< tex->getId() << endl;textureList[fileName];
	    return tex;
	 }
	 // else load
	 unsigned int texId = loadTexture(fileName);
	 Texture* tex = new Texture(texId);
	 textureList[fileName] = tex;
	 idList[texId] = tex;
			
	 cerr << "added texture id: " << texId << endl;
			
	 return idList[texId];
      }
		
      Texture* TextureManager::get(string fileName)
      {
	 std::map<std::string, Texture*>::const_iterator it = textureList.find(fileName);
	 if (it == textureList.end())
	 {
	    return add(fileName);
	 }
			
	 return (*it).second;
      }
		
      Texture* TextureManager::get(unsigned int id)
      {
	 return idList[id];
      }
		
   }
}
		


   
